home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / PartMaker 4.3 / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-12  |  9.6 KB  |  362 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Menu.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for application.            */
  28.  
  29. #ifndef __DESK__
  30. #include <Desk.h>
  31. #endif
  32.  
  33. #ifndef __ERRORS__
  34. #include <Errors.h>
  35. #endif
  36.  
  37. #ifndef __MEMORY__
  38. #include <Memory.h>
  39. #endif
  40.  
  41. #ifndef __MENUS__
  42. #include <Menus.h>
  43. #endif
  44.  
  45. #ifndef __TOOLUTILS__
  46. #include <ToolUtils.h>
  47. #endif
  48.  
  49. #ifndef __UTILITIES__
  50. #include "Utilities.h"
  51. #endif
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. extern Boolean    gQuitApplication;
  60. extern Boolean    gHasAppleEvents;
  61. extern OSType    gAppWindowType;
  62.  
  63. extern Boolean    gLowOnMem;
  64. extern short    gDialogErr;
  65.  
  66.  
  67.  
  68. /*****************************************************************************/
  69. /*****************************************************************************/
  70.  
  71. #ifdef applec
  72. #pragma segment Menu
  73. #endif
  74.  
  75. /*****************************************************************************/
  76. /*****************************************************************************/
  77.  
  78.  
  79.  
  80. /* •• Called by DTS.Lib..framework. •• */
  81.  
  82. /* Adjust the menu items.  We allow the DTS.Lib framework to do most of the work
  83. ** for us.  It will walk the menubar, and for each menu in the menubar, it will
  84. ** disable all of the menu items and then call the application at either
  85. ** AdjustMenuItems() (for document and palette windows, or for the no-window case),
  86. ** or DialogAdjustMenuItems() (for modal dialogs).  The application's job is to then
  87. ** turn on menu items that should be enabled to match the current application state.
  88. ** The initial Wannabe code for AdjustMenuItems() calls DoAdjustFileMenu() for the
  89. ** file menu, and DoAdjustEditMenu() for the edit menu.  Any other menus that may
  90. ** be added to Wannabe have all menu items enabled.  This allows menus to be added
  91. ** to the running version of Wannabe and allows them to actually do something.
  92. ** If the top-most window is a dialog, then all menus are disabled except for the
  93. ** edit menu.  Various items in the edit menu are enabled, depending on if there
  94. ** is an active TextEdit control, and what is in the clipboard. */
  95.  
  96. void    DoAdjustMenus(void)
  97. {
  98.     if (DoAdjustMBARMenus(FrontWindow(), rMenuBar))
  99.         DrawMenuBar();
  100. }
  101.  
  102.  
  103.  
  104. /*****************************************************************************/
  105.  
  106.  
  107.  
  108. /* This is called when an item is chosen from the menu bar (after calling
  109. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  110. ** It is good to have both the result of MenuSelect and MenuKey go to one
  111. ** routine like this to keep everything organized. */
  112.  
  113. Boolean    DoMenuCommand(short menuID, short menuItem)
  114. {
  115.     short        undoDepth, numUndos, saveMode, daRefNum;
  116.     Str255        daName;
  117.     FileRecHndl    frHndl;
  118.     WindowPtr    window;
  119.     TEHandle    te;
  120.     OSErr        err;
  121.     Boolean        handled;
  122.  
  123.     handled = true;
  124.  
  125.     window = FrontWindow();
  126.     if (window)
  127.         frHndl = (FileRecHndl)GetWRefCon(window);
  128.             /* frHndl is valid only if it is one of our windows. */
  129.  
  130.     switch (menuID) {
  131.  
  132.         case mApple:
  133.             switch (menuItem) {
  134.                 case kStdAbout:        /* Bring up alert for About. */
  135.                     NewDocumentWindow(nil, 'ABOT', false);
  136.                     break;
  137.                 default:            /* All non-About items in this menu are DAs. */
  138.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  139.                     daRefNum = OpenDeskAcc(daName);
  140.                     break;
  141.             }
  142.             break;
  143.  
  144.         case mFile:
  145.             switch (menuItem) {
  146.                 case kStdNew:
  147.                     gDialogErr = NewDocumentWindow(&frHndl, gAppWindowType, true);
  148.                     if (gDialogErr)
  149.                         NewDocumentWindow(nil, 'ERR#', false);
  150.                     break;
  151.                 case kStdOpen:
  152.                     err = OpenDocumentWindow(&frHndl, nil, fsRdPerm);
  153.                     if ((err) && (err != userCanceledErr)) {
  154.                         gDialogErr = err;
  155.                         NewDocumentWindow(nil, 'ERR#', false);
  156.                     }
  157.                     break;
  158.                 case kStdClose:
  159.                     if (IsAppWindow(window)) {
  160.                         window = FrontWindowOfType(kwIsDocument, true);
  161.                         if (window)
  162.                             DisposeOneWindow(window, kClose);
  163.                     }
  164.                     else
  165.                         DisposeOneWindow(window, kClose);        /* Dispose of DA window. */
  166.                     break;
  167.                 case kStdSave:
  168.                 case kStdSaveAs:
  169.                     saveMode = (menuItem == kStdSave) ? kSave : kSaveAs;
  170.                     if ((*frHndl)->fileState.refNum == kInvalRefNum)
  171.                         saveMode = kSaveAs;
  172.                     err = SaveDocument(frHndl, window, saveMode);
  173.                     if ((err) && (err != userCanceledErr)) {
  174.                         gDialogErr = err;
  175.                         NewDocumentWindow(nil, 'ERR#', false);
  176.                     }
  177.                     break;
  178.                 case kStdPageSetup:
  179.                     DoSetCursor(&qd.arrow);
  180.                     PresentStyleDialog(frHndl);
  181.                     break;
  182.                 case kStdPrint:
  183.                     DoSetCursor(&qd.arrow);
  184.                     err = noErr;
  185.                     if (!(*frHndl)->d.doc.fhInfo.printRecValid)
  186.                         err = PresentStyleDialog(frHndl);
  187.                     if (!err) {
  188.                         err = PrintDocument(frHndl, true, true);
  189.                         PrintDocument(nil, false, false);
  190.                     }
  191.                     if ((err) && (err != userCanceledErr)) {
  192.                         gDialogErr = err;
  193.                         NewDocumentWindow(nil, 'ERR#', false);
  194.                     }
  195.                     break;
  196.                 case kStdQuit:
  197.                     gQuitApplication = DisposeAllWindows();
  198.                     break;
  199.                 default:
  200.                     handled = false;
  201.                     break;
  202.             }
  203.             break;
  204.  
  205.         case mEdit:            /* Call SystemEdit for DA editing & MultiFinder. */
  206. #if VH_VERSION
  207.             if (menuItem == kStdViewHier) {
  208.                 handled = false;
  209.                 break;
  210.             }
  211. #endif
  212.             if (IsAppWindow(window)) {
  213.                 switch (menuItem) {
  214.                     case kStdUndo:
  215.                     case kStdRedo:
  216.                     case kStdCut:
  217.                     case kStdCopy:
  218.                     case kStdPaste:
  219.                     case kStdClear:
  220.                         switch ((*frHndl)->fileState.sfType) {
  221.                                 /* This is written with the assumption that document types
  222.                                 ** that demand specific code will be added.  The below “if”
  223.                                 ** illustrates how to handle the edit menu for windows that
  224.                                 ** have an active TextEdit control.  The “else” shows a typical
  225.                                 ** undo/redo scenario for applications that are using the
  226.                                 ** hierarchical document package.  The clipboard features
  227.                                 ** are of course document-dependent, so a sample hasn't been
  228.                                 ** implemented here.  For a sample, see DTS.Draw. */
  229.                             default:
  230.                                 te = CTEFindActive(window);
  231.                                 if (te) {
  232.                                     if ((*te)->viewRect.left < -8192)
  233.                                         BeginFrame(window);
  234.                                     else
  235.                                         BeginContent(window);
  236.                                     if (menuItem == kStdUndo)
  237.                                         CTEUndo();
  238.                                     else
  239.                                         CTEClipboard(menuItem - kStdCut + 2);
  240.                                     EndContent(window);
  241.                                 }
  242.                                 else {
  243.                                     if (menuItem <= kStdRedo) {
  244.                                         if (!UnmapMItem(mEdit, kStdUndo)) {
  245.                                             GetUndoInfo(frHndl, &undoDepth, &numUndos);
  246.                                             DoUndoTask((*frHndl)->d.doc.root, 1 - undoDepth, true);
  247.                                         }
  248.                                         else DoUndoTask((*frHndl)->d.doc.root, menuItem - kStdUndo, true);
  249.                                     }
  250.                                     else {
  251.                                         /* Handle rest of edit menu here. */
  252.                                     }
  253.                                 }
  254.                                 break;
  255.                         }
  256.                         break;
  257.                 }
  258.             }
  259.             else SystemEdit(menuItem - 1);
  260.             break;
  261.  
  262.         default:
  263.             handled = false;
  264.             break;
  265.  
  266.     }
  267.  
  268.     return(handled);
  269. }
  270.  
  271.  
  272.  
  273. /*****************************************************************************/
  274.  
  275.  
  276.  
  277. Boolean    DoAdjustFileMenu(WindowPtr window)
  278. {
  279.     MenuHandle    menu;
  280.     FileRecHndl    frHndl;
  281.     short        enableItem;
  282.  
  283.     menu = GetMenuHandle(mFile);
  284.     EnableItem(menu, UnmapMItem(mFile, kStdQuit));            /* Gotta be able to quit. */
  285.  
  286.     if (IsDAWindow(window)) {
  287.         EnableItem(menu, UnmapMItem(mFile, kStdClose));        /* Let DAs do a close from the menu. */
  288.         return(false);
  289.     }
  290.  
  291.     if (!gLowOnMem) {
  292.         EnableItem(menu, UnmapMItem(mFile, kStdNew));
  293.         EnableItem(menu, UnmapMItem(mFile, kStdOpen));
  294.     }
  295.  
  296.     window = FrontWindowOfType(kwIsDocument, true);
  297.     if (window) {
  298.         EnableItem(menu, UnmapMItem(mFile, kStdClose));
  299.         frHndl = (FileRecHndl)GetWRefCon(window);
  300.         if ((*frHndl)->fileState.sfType == 'PtMd') {
  301.             enableItem = GetWindowDirty(window);
  302.             if ((*frHndl)->fileState.refNum == kInvalRefNum)
  303.                 enableItem = true;
  304.             if (enableItem)
  305.                 EnableItem(menu, UnmapMItem(mFile, kStdSave));
  306.             EnableItem(menu, UnmapMItem(mFile, kStdSaveAs));
  307.         }
  308.         EnableItem(menu, UnmapMItem(mFile, kStdPageSetup));
  309.         EnableItem(menu, UnmapMItem(mFile, kStdPrint));
  310.     }
  311.  
  312.     return(false);
  313. }
  314.  
  315.  
  316.  
  317. /*****************************************************************************/
  318.  
  319.  
  320.  
  321. Boolean    DoAdjustEditMenu(WindowPtr window)
  322. {
  323.     MenuHandle        menu;
  324.     Boolean            menuEnabled;
  325.     FileRecHndl        frHndl;
  326.  
  327.     menu = GetMenuHandle(mEdit);
  328.  
  329.     if (IsDAWindow(window)) {
  330.         EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
  331.         EnableItem(menu, UnmapMItem(mEdit, kStdCut));
  332.         EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
  333.         EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
  334.         EnableItem(menu, UnmapMItem(mEdit, kStdClear));
  335.         return(false);
  336.     }
  337.  
  338.     if (IsAppWindow(window)) {
  339.         frHndl = (FileRecHndl)GetWRefCon(window);
  340.         switch ((*frHndl)->fileState.sfType) {
  341. #if VH_VERSION
  342.             case kViewHierFileType:
  343.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  344.                                                  UnmapMItem(mEdit, kStdCut));
  345.                 break;
  346. #endif
  347.             default:
  348. #if VH_VERSION
  349.                 EnableItem(menu, UnmapMItem(mEdit, kStdViewHier));
  350. #endif
  351.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  352.                                                  UnmapMItem(mEdit, kStdCut));
  353.                 break;
  354.         }
  355.     }
  356.  
  357.     return(false);
  358. }
  359.  
  360.  
  361.  
  362.